package com.ssh.controller.consumer;

import com.common.service.CommonService;
import com.ssh.entity.Consumer;
import com.ssh.service.ConsumerService;
import com.common.tool.Message;
import com.common.tool.Page;
import com.common.tool.PagingTool;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by zpen on 2018/2/24.
 */
@Controller
@RequestMapping("consumer.action")
public class ConsumerController {
    @Autowired
    ConsumerService personSerivce;

    @Autowired
    CommonService commonService;



    /**
     * 跳转用户列表
     *
     * @param request
     * @param model
     * @return
     */
    @RequestMapping()
    public String goConsumer(HttpServletRequest request, Model model) {
        model.addAttribute("ctx",request.getContextPath());
      return "consumer/list";
    }

    /**
     * 获取用户信息
     *
     * @return
     */
    @RequestMapping(params = "method=getDate")
    @ResponseBody
    public  PagingTool getConsumer(Consumer consumer, Page page){
        PagingTool pagingTool = personSerivce.queryForPage(consumer,page);
        return pagingTool;
    }

    /**
     * 保存用户
     *
     * @return
     */
    @RequestMapping(params = "method=save",method = RequestMethod.POST)
    @ResponseBody
    public Map<String,String> saveConsumer(Consumer consumer){
        Map<String,String> map = new HashMap<>();
        try{
        Long conId = personSerivce.saveConsumer(consumer);
        if(conId!=null){
            map.put("msg","success");
        }else {
            map.put("msg","fail");
        }
        }catch (Exception e){
            map.put("msg","exc");
            e.printStackTrace();
        }
        return  map;
    }

    /**
     * 跳转到添加用户
     *
     * @param request
     * @param model
     * @return
     */
    @RequestMapping(params = "method=add")
    public String goAdd(HttpServletRequest request, Model model){
        model.addAttribute("ctx",request.getContextPath());
        return "consumer/created";
    }

    /**
     * 删除用户信息
     * @param id
     * @return
     */
    @RequestMapping(params = "method=delete")
    @ResponseBody
    public Message delConsumer(String id,String mode){
        Message msg = new Message();
        try{
          Message message =  personSerivce.delConsumer(id,mode);
          if("repairIsNoNot".equals(message.getSuccess())){
              msg.setSuccess("fail");
              msg.setMsg("该用户有未完成的订单，请确认信息再删除！");
          }else if("exception".equals(message.getSuccess())){
              msg.setSuccess("fail");
              msg.setMsg("删除用户异常！请联系管理员处理！");
          }else{
              msg.setSuccess("success");
          }
        }catch (Exception e){
            msg.setSuccess("fail");
            msg.setMsg(e.toString());
        }
        return msg;
    }


}
